﻿Public Class 参数设置

#Region "窗体操作"

    '关闭窗体
    Private Sub 参数设置_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        SettRPForm = Nothing
    End Sub
    '关闭窗口
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Me.Close()
    End Sub

    '窗体加载
    Private Sub 参数设置_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        '加载维修员表
        RPpeople()
        '加载维修项表
        RPeventT()

        D1_LostFocus()

    End Sub
    '加载维修员表
    Private Sub RPpeople()
        Dim sqlstr As String = "select 维修员 from 维修员"
        Dim ds As DataSet = SQLtoDataSet(sqlstr)
        Dim aa As Integer = ds.Tables(0).Rows.Count
        If aa > 0 Then
            ListBox1.Items.Clear()
            For i = 0 To aa - 1
                ListBox1.Items.Add(Trim(ds.Tables(0).Rows(i).Item(0).ToString))
            Next
        End If
    End Sub
    '加载维修项表
    Private Sub RPeventT()
        Dim sqlstr As String = "select ID ,项目,预测费用 from 维修项"
        Dim ds As DataSet = SQLtoDataSet(sqlstr)
        Dim aa As Integer = ds.Tables(0).Rows.Count
        If aa > 0 Then
            DataGridView1.Rows.Clear()
            For i = 0 To aa - 1
                Dim rs As DataRow = ds.Tables(0).Rows(i)
                Dim row1() As String = {rs.Item("ID").ToString, Trim(rs.Item("项目").ToString), rs.Item("预测费用").ToString}
                DataGridView1.Rows.Add(row1)
            Next
        End If
    End Sub

#End Region

#Region "数据操作"

    '添加维修人员
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Trim(TextBox1.Text) <> "" Then
            Dim sqlstr As String = "insert into 维修员(维修员)values('" + Trim(TextBox1.Text) + "')"
            SQLupdata(sqlstr)
            RPpeople()
            TextBox1.Text = ""
        Else
            MsgBox("请输入名称！")
            TextBox1.Focus()
        End If
        TextBox1.Focus()
    End Sub

    '删除维修人员
    Private Sub 删除ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 删除ToolStripMenuItem.Click
        Dim ii As Integer = ListBox1.SelectedIndex

        Dim sqlstr As String = "delete from 维修员 where 维修员 = '" + ListBox1.SelectedItem.ToString + "'"
        SQLupdata(sqlstr)
        ListBox1.Items.Clear()
        RPpeople()

        If ii - 1 > 0 Then
            ListBox1.SelectedIndex = ii - 1
        Else
            ListBox1.ClearSelected()
        End If



    End Sub

    '添加维修人项目
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        If Trim(TextBox2.Text) <> "" Then
            Dim sqlstr As String = ""
            If Trim(TextBox3.Text) = "" Then
                sqlstr = "insert into 维修项(项目)values('" + Trim(TextBox2.Text) + "')"
            Else
                sqlstr = "insert into 维修项(项目,预测费用)values('" + Trim(TextBox2.Text) + "','" + TextBox3.Text + "')"
            End If

            SQLupdata(sqlstr)
            RPeventT()
            TextBox2.Text = ""
            TextBox3.Text = ""

        Else
            MsgBox("请输入维修项目！")
            TextBox2.Focus()
        End If

        D1_LostFocus()
        TextBox2.Focus()

    End Sub

    '删除维修人项目
    Private Sub 删除ToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 删除ToolStripMenuItem1.Click
        Dim ii As Integer = DataGridView1.SelectedRows(0).Index

        Dim sqlstr As String = "delete from 维修项 where ID = " + DataGridView1.SelectedRows(0).Cells(0).Value.ToString + ""
        SQLupdata(sqlstr)
        DataGridView1.Rows.Clear()
        RPeventT()

        '选定行
        If ii - 1 >= 0 Then
            DataGridView1.Rows(ii - 1).Selected = True
        End If

    End Sub

#End Region

    '只输入数字
    Private Sub TextBox3_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox3.KeyPress
        NumOnly(e)
    End Sub
    '只输入数字
    Private Sub NumOnly(ByVal e As System.Windows.Forms.KeyPressEventArgs)
        If e.KeyChar >= "0" And e.KeyChar <= "9" Or e.KeyChar = Chr(Keys.Back) Or e.KeyChar = Chr(46) Then  '只允许输入数字与点
            e.Handled = False           '当按键不合法时　则不允许输入
        Else
            e.Handled = True            '当按键合法时　则允许输入并显示
        End If
    End Sub

    '去选定
    Private Sub D1_LostFocus()

        Dim ii As Integer = DataGridView1.Rows.Count
        For i = 0 To ii - 1
            DataGridView1.Rows(i).Selected = False
        Next

    End Sub
    Private Sub DataGridView1_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView1.LostFocus
        D1_LostFocus()
    End Sub
    Private Sub ListBox1_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.LostFocus
        ListBox1.ClearSelected()
    End Sub


End Class